package servlet;

import dao.AddressDao;
import entity.Address;
import entity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/addAddress2")
public class AddAddress2Servlet extends HttpServlet {
	
	@Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        doGet(request, response);
    }
	@Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {

		System.out.println("-----------addAddress2------------");
		User user = (User) request.getSession().getAttribute("user");
		String recipientName = request.getParameter("recipientName");
		String recipientPhone = request.getParameter("recipientPhone");
		String province = request.getParameter("s_province");
		String city = request.getParameter("s_city");
		String county = request.getParameter("s_county");
		String detailAddress = request.getParameter("detailAddress");
		String address = city + county + detailAddress;

		Address add = new Address();
		add.setProvince(province);
		add.setCity(city);
		add.setCounty(county);
		add.setDetailAddress(detailAddress);
		add.setRecipientName(recipientName);
		add.setRecipientPhone(recipientPhone);
		add.setUserId(user.getUserId());
		add.setAddress(address);
		AddressDao dao = new AddressDao();
		int ret = dao.addToAddress(add);
		if(ret == -1) {
			response.sendRedirect("error.jsp");
			return;
		}else {
			AddressDao addressDao = new AddressDao();
			List<Address> list = addressDao.selectMyAddress(user.getUserId());
			if(list == null) {
				response.sendRedirect("error.jsp");
				return;
			}else {
				request.getSession().setAttribute("myAddresses", list);
				request.getSession().setAttribute("successMsg", "收货地址添加成功！");
				response.sendRedirect("frontend/jiesuan.jsp");
			}
		}

	}


}
